/**
 * JAVACC DEMO 1.0
 * @Package com.apache.passport.common
 */
package com.apache.passport.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SpringContextLoader;
import com.apache.passport.entity.BlackRoster;
import com.apache.passport.entity.LonErrToken;
import com.apache.passport.entity.UctUser;
import com.apache.passport.manager.BlackRosterManager;

/**
 * description:  登录错误缓存计时器
 */
public class LoginCacheErrTimer {

    private static LoginCacheErrTimer instance;

    private LoginCacheErrTimer() {
    }

    public static LoginCacheErrTimer getInstance() {
        if (null == instance) {
            instance = new LoginCacheErrTimer();
        }
        return instance;
    }

    private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private BlackRosterManager blackRosterManager = (BlackRosterManager) SpringContextLoader
            .getBean("blackRosterManager");

    /***
     * description:  设置黑名单及错误计数器的token方法
     * @param vo
     * @param remoteIp
     * @param br
     * @param obj
     * @return
     * @throws ParseException  
     * @author Hou Dayu 
     * @update 2014-12-24
     */
    public Object setErrToken(ParamsVo vo, String remoteIp, BlackRoster br, UctUser obj)
            throws ParseException {

        //各自控制自己的错误token,因在登录成功后,这个token将会失效,使用Ip做key,需要存储时间,用于比较下次登录的时间
        String userEname = obj.getUserEname();
        LonErrToken token = PassPortConst.getLonErrToken(remoteIp, userEname);
        if (!Validator.isEmpty(token)) {
            String count = token.getLoginCount();
            int errcount = Integer.parseInt(count);//12
            Date d1 = df.parse(df.format(new Date()));
            Date d2 = df.parse(token.getLoginTime());
            long diff = d1.getTime() - d2.getTime();
            //分钟
            long midd = diff / 1000 / 60;
            //当错误次数为3次
            if (errcount == 3) {
                //如果时间已经过去10分钟了,则向缓存中写时间
                if (midd >= 10) {
                    errcount++;
                    //存时间,用于判断
                    token.setLoginTime(df.format(new Date()));
                    token.setLoginCount(errcount + "");
                }
            } else if (errcount == 6) {
                //如果时间已经过去半小时了,则向缓存中写时间
                if (midd >= 30) {
                    errcount++;
                    //存时间,用于判断
                    token.setLoginTime(df.format(new Date()));
                    token.setLoginCount(errcount + "");
                }
            } else if (errcount == 9) {
                //如果时间已经过去24小时了,则向缓存中写时间
                if (midd >= 60 * 24) {
                    errcount++;
                    //存时间,用于判断
                    token.setLoginTime(df.format(new Date()));
                    token.setLoginCount(errcount + "");
                }
            } else if (errcount == 12) {
                ParamsVo<BlackRoster> arg0 = new ParamsVo<BlackRoster>();
                br = new BlackRoster();
                br.setBrCreatetime(df.format(new Date()));
                br.setBrIpaddress(remoteIp);
                br.setBrRemark("密码登录错误超过12次");
                br.setUserEname("PASSPORT");
                br.setBrAccount(userEname);
                arg0.setObj(br);
                String brId = blackRosterManager.saveInfo(arg0);
                if (!Validator.isNull(brId)) {
                    token.setLoginCount("12");
                }
            } else {
                errcount++;
                //存时间,用于判断
                token.setLoginTime(df.format(new Date()));
                token.setLoginCount(errcount + "");
            }

        } else {
            //第一次,创建对象
            token = new LonErrToken();
            //存时间,用于判断
            token.setLoginTime(df.format(new Date()));
            token.setCacheId(remoteIp + userEname);
            token.setUserEname(userEname);
            token.setLoginCount("1");
        }
        //存储缓存
        PassPortConst.setLonErrToken(remoteIp, userEname, token);
        return null;
    }


    public boolean checkErrToken(String remoteIp,String userEname) throws ParseException {
        LonErrToken token = PassPortConst.getLonErrToken(remoteIp, userEname);
        if (!Validator.isEmpty(token)) {
            String count = token.getLoginCount();
            int errcount = Integer.parseInt(count);//12
            Date d1 = df.parse(df.format(new Date()));
            Date d2 = df.parse(token.getLoginTime());
            long diff = d1.getTime() - d2.getTime();
            //分钟
            long midd = diff / 1000 / 60;
            //当错误次数为3次
            if (errcount <= 3 && midd >= 10) {//三次超过10分钟
                return false;
            } else if (errcount > 3 && errcount <= 6 && midd >= 30) {//6次时间已经过去半小时
                return false;
            } else if (errcount > 6 && errcount <= 9 && midd >= 60 * 24) {//9次时间已经过去24小时
                return false;
            } else if (errcount > 9) {
                return true;
            }
        }
        return true;
    }
}
